package com.yonder.service.impl;

import com.yonder.dao.SetmealDao;
import com.yonder.pojo.Setmeal;
import com.yonder.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName SetmealServiceImpl
 * @Description TODO
 * @Author zxs
 * @Date 2021/3/21 9:49
 * Version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public void setMealAdd(Setmeal setmeal, Integer[] checkGroupIds) {
//        setmealDao.add(setmeal,checkGroupIds);
        setmealDao.setMealAdd(setmeal);
        Integer setMealId = setmeal.getId();
        setCheckGroupAndSetmeal(checkGroupIds, setMealId);
//        for (Integer checkGroupId : checkGroupIds) {
//            setmealDao.addSetmealCheckgroup(checkGroupId,setMealId);
//        }

    }

    /**
     * 新增套餐 编辑套餐 中间表 公共方法
     *
     * @param checkgroupIds
     * @param setmealId
     */
    private void setCheckGroupAndSetmeal(Integer[] checkgroupIds, Integer setmealId) {
        if (checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                Map map = new HashMap<>();
                map.put("setmealId", setmealId);
                map.put("checkgroupId", checkgroupId);
                //中间表插入关系
                setmealDao.addSetmealCheckgroup(map);
            }

        }
    }
}